package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.risk;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.QuotaConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;

@Component
public class Lbt036InsuranceRiskStrategy implements InsuranceRiskStrategy {

    private final InsuredCoreRepository insuredCoreRepository;

    private final RedisService redisService;

    public Lbt036InsuranceRiskStrategy(InsuredCoreRepository insuredCoreRepository, RedisService redisService) {
        this.insuredCoreRepository = insuredCoreRepository;
        this.redisService = redisService;
    }

    @Override
    public InsuredModel checkProductRiskQuota(ProductModel product, RequestModel requestModel) {

        /**
         * 被保险人年龄小于等于50岁，且累计险种保费大于xx万，不能投保；
         * 被保险人年龄大于50岁，且累计险种保费大于xx万，不能投保。（待再保确认）
         */

        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(product.getProdInsSeq(), requestModel.getCustInfo().getInsurants().getInsurant());
        Date valiDate = requestModel.getPolicyInfo().getCvalidate();
        int years = DateUtil.getYears(insurantModel.getBirthday(), valiDate);

        BigDecimal currentPrem = product.getPrem();

        //缓存中可能存在的保额
        BigDecimal cacheQuota = redisService.getCacheQuota(QuotaConstant.SORT_LBT036, insurantModel.getIdNo(), currentPrem, requestModel.getPolicyInfo().getContNo());

        BigDecimal historicalSumPrem = StringUtils.isNotBlank(insurantModel.getInsuredNo()) &&
                !StringUtils.equals(AcceptConstant.MINUS_ONE, insurantModel.getInsuredNo()) ?
                insuredCoreRepository.getHistoricalSumPrem(insurantModel.getInsuredNo(), product.getProdCode()) : BigDecimal.valueOf(0);
        //累计险种保费
        BigDecimal total = currentPrem.add(cacheQuota).add(historicalSumPrem);

        if (years >= UnderWriteConstant.AGE_EIGHTEEN_YEAR && years <= UnderWriteConstant.AGE_FIFTY_YEAR) {
            //total.compareTo(BigDecimal.valueOf(UnderWriteConstant.ONE_HUNDRED_THOUSAND)) > 0 &&
            if (total.compareTo(BigDecimal.valueOf(2 * UnderWriteConstant.ONE_MILLION)) <= 0) {
                if (StringUtils.equals(AcceptConstant.STRING_Y, insurantModel.getInsInform())) {
                    return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                            String.format(UnderWriteConstant.UNDER_WRITE_NYGG0053_MESSAGE, insurantModel.getName(), years));
                }
            } else if (total.compareTo(BigDecimal.valueOf(2 * UnderWriteConstant.ONE_MILLION)) > 0) {
                return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NYGG0054_MESSAGE, insurantModel.getName(), years));
            }
        } else if (years > UnderWriteConstant.AGE_FIFTY_YEAR) {
            if (StringUtils.equals(AcceptConstant.STRING_Y, insurantModel.getInsInform()) &&
                    total.compareTo(BigDecimal.valueOf(3 * UnderWriteConstant.FIVE_HUNDRED_THOUSAND)) <= 0) {
                return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NYGG0053_MESSAGE, insurantModel.getName(), years));
            } else if (total.compareTo(BigDecimal.valueOf(3 * UnderWriteConstant.FIVE_HUNDRED_THOUSAND)) > 0) {
                return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_FAIL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NYGG0055_MESSAGE, insurantModel.getName(), years));
            }
        }

        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
